<?php
/**
 * 系统消息
 * Date: 2020/7/14
 * Author: sunlinxin
 */
namespace Frieza\Modules\Sysmsg\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\model\Sysmsg;

class SysmsgController extends Controller
{
    /**
     * 消息列表视图
     * @param Request $request
     * @param Content $content
     * @return Content
     */
    public function index(Request $request,Content $content)
    {
        $params = $request->input();
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);

        $search = [];
        if (isset($params['from_uid']) && $params['from_uid']) {
            $search['from_uid'] = $params['from_uid'];
        }
        if (isset($params['to_uid']) && $params['to_uid']) {
            $search['to_uid'] = $params['to_uid'];
        }
        if (isset($params['msg_code']) && $params['msg_code']) {
            $search['msg_code'] = $params['msg_code'];
        }
        $list = Sysmsg::getSysMsgPageList($page, $pageSize, $search);
        $gridModel = new pGridDataModel($list['list'], $list['total'], [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->filter(function($filter){
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->like('from_uid', '发送者Uid');
                    $filter->like('to_uid', '接收者Uid');
                    $filter->like('msg_code', '消息code码');
                });

                $grid->column("id", 'ID');
                $grid->column("to_uid", '接收者Uid');
                $grid->column("from_uid", '发送者Uid');
                $grid->column("title", '标题');
                $grid->column("img", '图片')->image();
                $grid->column("link", '链接');
                $grid->column("detail", '详情');
                $grid->column("float_text", '图片浮层');
                $grid->column("msg_code", '消息code码');
                $grid->column("type", '消息类型')->using([
                    Sysmsg::TYPE_SYS_MSG => '系统消息',
                    Sysmsg::TYPE_NOTICE => '通知'
                ]);
                $grid->column("send_time", '发送时间');
                $grid->column("status", '状态')->using([
                    Sysmsg::STATUS_NORMAL=>'正常',
                    Sysmsg::STATUS_DEL=>'删除'
                    ]);
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableEdit();
                });
            });

        $tab = new Tab();
        $tab->add('系统消息列表', $gridList->render());
        $content->row($tab);
        return $content
            ->header('系统消息列表')
            ->description(' ')
            ->breadcrumb(['text' => '系统消息列表']);
    }

    /**
     * 删除行记录
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($id = 0)
    {
        if (!$id) {
            return response()->json(['status' => false, 'message' => "操作失败"]);
        }
        $res = Sysmsg::updateData(['status'=>Sysmsg::STATUS_DEL],['id'=>$id]);
        if ($res) {
            return response()->json(['status' => true, 'message' => "操作成功"]);
        } else {
            return response()->json(['status' => false, 'message' => "操作失败"]);
        }
    }
}